package com.example.stroebackstage.dao;
import com.example.stroebackstage.entity.Administrator;
import com.example.stroebackstage.utils.LinkMySQL;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Objects;

public class AdministratorDao {
    //    增
    public static boolean insertAdmin(Administrator administrator){
        Connection conn = null;
        PreparedStatement pstmt = null;
        int numb = 0;
        try {
            conn = LinkMySQL.getConnection();
            String sql = "insert into admin(adminName,adminPassword,roleId)values(?,?,?)";
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1,administrator.getAdminName());
            pstmt.setString(2,administrator.getPassword());
            pstmt.setInt(3,administrator.getRoleId());
            numb = pstmt.executeUpdate();
        }catch (Exception e) {
            e.printStackTrace();
        }finally {
            LinkMySQL.release(pstmt,conn);
        }
        return numb>0;
    }
    //    删
    public static boolean delAdmin(int id){
        Connection conn = null;
        PreparedStatement pstmt = null;
        int numb = 0;
        try {
            conn = LinkMySQL.getConnection();
            String sql =
                    "delete from admin where adminId=?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1,id);
            numb = pstmt.executeUpdate();
        }catch (Exception e) {
            e.printStackTrace();
        }finally {
            LinkMySQL.release(pstmt,conn);
        }
        return numb>0;
    }
    //    改
    public static boolean updateAdmin(Administrator administrator){
        Connection conn = null;
        PreparedStatement pstmt = null;
        int numb = 0;
        try {
            conn = LinkMySQL.getConnection();
            String sql =
                    "update admin set adminName=? ,adminPassword=?,roleId=? where adminId=?" ;
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1,administrator.getAdminName());
            pstmt.setString(2,administrator.getPassword());
            pstmt.setInt(3,administrator.getRoleId());
            pstmt.setInt(4,administrator.getAdminId());
            numb = pstmt.executeUpdate();
        }catch (Exception e) {
            e.printStackTrace();
        }finally {
            LinkMySQL.release(pstmt,conn);
        }
        return numb>0;
    }
    public static boolean updateAdmin(int id,String name){
        Connection conn = null;
        PreparedStatement pstmt = null;
        int numb = 0;
        try {
            conn = LinkMySQL.getConnection();
            String sql =
                    "update admin set adminName=? where adminId=?" ;
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1,name);
            pstmt.setInt(2,id);
            numb = pstmt.executeUpdate();
        }catch (Exception e) {
            e.printStackTrace();
        }finally {
            LinkMySQL.release(pstmt,conn);
        }
        return numb>0;
    }
    public static boolean updateAdmin(int aid,int rid){
        Connection conn = null;
        PreparedStatement pstmt = null;
        int numb = 0;
        try {
            conn = LinkMySQL.getConnection();
            String sql =
                    "update admin set roleId=? where adminId=?" ;
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1,rid);
            pstmt.setInt(2,aid);
            numb = pstmt.executeUpdate();
        }catch (Exception e) {
            e.printStackTrace();
        }finally {
            LinkMySQL.release(pstmt,conn);
        }
        return numb>0;
    }
    //    查
    public static ArrayList<Administrator> selectAdmin(){
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet res = null;
        ArrayList<Administrator> administrators = new ArrayList<>();
        try {
            conn = LinkMySQL.getConnection();
            String sql = "select a.*,r.roleName from admin as a left join role as r on a.roleId=r.roleId;";
            pstmt = Objects.requireNonNull(conn).prepareStatement(sql);
            res = pstmt.executeQuery();
            while (res.next()){
                Administrator administrator = new Administrator(
                        res.getInt("a.adminId"),res.getString("a.adminName"),res.getString("a.adminPassword"),res.getString("r.roleName"));
                administrators.add(administrator);
            }
        }catch (Exception e) {
            e.printStackTrace();
        }finally {
            LinkMySQL.release(res,pstmt,conn);
        }
        return administrators;
    }
    public static ArrayList<Administrator> selectAdmin(int id){
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet res = null;
        ArrayList<Administrator> administrators = new ArrayList<>();
        try {
            conn = LinkMySQL.getConnection();
            String sql = "select a.*,r.roleName from admin as a left join role as r on a.roleId=r.roleId where a.adminId=?";
            pstmt = Objects.requireNonNull(conn).prepareStatement(sql);
            pstmt.setInt(1,id);
            res = pstmt.executeQuery();
            while (res.next()){
                Administrator administrator = new Administrator(
                        res.getInt("a.adminId"),res.getString("a.adminName"),res.getString("a.adminPassword"),res.getString("r.roleName"));
                administrators.add(administrator);
            }
        }catch (Exception e) {
            e.printStackTrace();
        }finally {
            LinkMySQL.release(res,pstmt,conn);
        }
        return administrators;
    }
    public static ArrayList<Administrator> selectAdmin(String name){
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet res = null;
        ArrayList<Administrator> administrators = new ArrayList<>();
        try {
            conn = LinkMySQL.getConnection();
            String sql = "select a.*,r.roleName from admin as a left join role as r on a.roleId=r.roleId where a.adminName like ?";
            pstmt = Objects.requireNonNull(conn).prepareStatement(sql);
            pstmt.setString(1,"%"+name+"%");
            res = pstmt.executeQuery();
            while (res.next()){
                Administrator administrator = new Administrator(
                        res.getInt("a.adminId"),res.getString("a.adminName"),res.getString("a.adminPassword"),res.getString("r.roleName"));
                administrators.add(administrator);
            }
        }catch (Exception e) {
            e.printStackTrace();
        }finally {
            LinkMySQL.release(res,pstmt,conn);
        }
        return administrators;
    }
    //    验证是否已经存在用户名
    public static boolean checkName(String name ){
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet res = null;
        boolean check = false;
        try {
            conn = LinkMySQL.getConnection();
            String sql = "select * from admin where adminName = ?";
            pstmt = Objects.requireNonNull(conn).prepareStatement(sql);
            pstmt.setString(1,name);
            res = pstmt.executeQuery();
            if(res.next()){
                check = true;
            }
        }catch (Exception e) {
            e.printStackTrace();
        }finally {
            LinkMySQL.release(res,pstmt,conn);
        }
        return check;
    }
    //    验证用户名与密码
    public static Administrator selectAdmin(String name, String password){
//        Connection conn = null;
//        PreparedStatement pstmt = null;
//        ResultSet res = null;
//        Administrator administrator = null;
//        try {
//            conn = LinkMySQL.getConnection();
//            String sql = "select * from admin where adminName=?and adminPassword=?";
//            pstmt = Objects.requireNonNull(conn).prepareStatement(sql);
//            pstmt.setString(1,name);
//            pstmt.setString(2,password);
//            res = pstmt.executeQuery();
//            if(res.next()){
//                administrator = new Administrator(
//                        res.getInt("adminId"),res.getString("adminName"),res.getString("adminPassword"),res.getInt("roleId"));
//            }
//        }catch (Exception e) {
//            e.printStackTrace();
//        }finally {
//            LinkMySQL.release(res,pstmt,conn);
//        }

//        return administrator;
        return new Administrator("lin","jjjj",1);
    }
}
